package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Enterprise;
import com.fsnbigdata.domain.QEnterprise;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = EnterpriseRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = EnterpriseRepositoryCustom.class)
@Transactional(readOnly = true)
public class EnterpriseRepositoryImpl extends QueryDslRepositorySupportExt<Enterprise> implements EnterpriseRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    EnterpriseRepositoryImpl() {
        super(Enterprise.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANIZATION_CODE = "organizationCode";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LEGAL_PERSON = "legalPerson";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY = "category";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHONE = "phone";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ADDRESS = "address";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String POSTAL = "postal";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String FAX = "fax";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String EMAIL = "email";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String WEBSITE = "website";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";
    
    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TYPE = "type";

    @Override
    public Page<Enterprise> findAll(Predicate predicate, Pageable pageable) {
        QEnterprise enterprise = QEnterprise.enterprise;
        JPQLQuery<Enterprise> query = from(enterprise).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, enterprise);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    @Override
    public Page<Enterprise> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QEnterprise enterprise = QEnterprise.enterprise;
        JPQLQuery<Enterprise> query = from(enterprise);
        if(creator != null ){
            query.where(enterprise.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { enterprise.name, enterprise.category, enterprise.legalPerson, enterprise.postal, enterprise.phone, enterprise.fax, enterprise.email, enterprise.website, enterprise.address, enterprise.organizationCode, enterprise.creator ,enterprise.type};
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, enterprise.name).map(CATEGORY, enterprise.category).map(LEGAL_PERSON, enterprise.legalPerson).map(POSTAL, enterprise.postal).map(PHONE, enterprise.phone).map(FAX, enterprise.fax).map(EMAIL, enterprise.email).map(WEBSITE, enterprise.website).map(ADDRESS, enterprise.address).map(ORGANIZATION_CODE, enterprise.organizationCode).map(CREATOR, enterprise.creator).map(TYPE,enterprise.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, enterprise);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    @Override
    public Page<Enterprise> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QEnterprise enterprise = QEnterprise.enterprise;
        JPQLQuery<Enterprise> query = from(enterprise);
        Path<?>[] paths = new Path<?>[] { enterprise.name, enterprise.category, enterprise.legalPerson, enterprise.postal, enterprise.phone, enterprise.fax, enterprise.email, enterprise.website, enterprise.address, enterprise.organizationCode, enterprise.creator ,enterprise.type};
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(enterprise.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, enterprise.name).map(CATEGORY, enterprise.category).map(LEGAL_PERSON, enterprise.legalPerson).map(POSTAL, enterprise.postal).map(PHONE, enterprise.phone).map(FAX, enterprise.fax).map(EMAIL, enterprise.email).map(WEBSITE, enterprise.website).map(ADDRESS, enterprise.address).map(ORGANIZATION_CODE, enterprise.organizationCode).map(CREATOR, enterprise.creator).map(TYPE,enterprise.type);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, enterprise);
    }
}
